﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;

namespace map.member
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)

        {
            Button1.Visible = false;
            showWarning.Visible = true;
            subPrice.Visible = false;
            GridViewBind();
        }
        private void GridViewBind()
        {
            //不显示取消的订阅
            RuleCode.Table.cbBroker x = RuleCode.Table.cbBroker.Find(" ShopOwnerID = '" + AliApi.appUserId + "' and NoYes=1");
            this.GridView1.DataSource = x.MyTable;

            this.GridView1.DataBind();
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            RuleCode.Table.cbuser x = RuleCode.Table.cbuser.Find(" (cbuserid = '" + Searchbox.Text + "'or cbusernickname = '"+Searchbox.Text+"') and BokerStatus = 2");
            if (x.MyTable.Rows.Count>0)
            {
                Button1.Visible = true;
                showWarning.Visible = true;

                brokerName.Text = x.cbuserid.StringValue;
                subPrice.Text = "价格：" + x.BrokerPrice.StringValue + "元";
                hideAmount.Text = x.BrokerPrice.StringValue;
                subPrice.Visible = true;
            }
            else
            {
                Label1.Text = "没有找到经纪人";
            }
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //审核通过
            string id = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
            cbBroker uc = cbBroker.Find(" recid = '" + id + "'", "RECID");

            if (uc.NoYes == RuleCode.Enum.NoYes.No)
            {
                return;
            }
            else
            {
                if (uc.NoYes == RuleCode.Enum.NoYes.Yes)
                {
                    uc.NoYes = RuleCode.Enum.NoYes.No;
                    uc.Modify();
                }

            }

            GridViewBind();



        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            var user = new cbBroker();
            doDeal();
            user.Init(0);
            user.New();

            
            user.ShopOwnerID = AliApi.appUserId;
            user.BrokerID = brokerName.Text;
            user.StartDate = DateTime.Today;
            user.NoYes = RuleCode.Enum.NoYes.Yes;
            user.Amount.StringValue = hideAmount.Text;
            user.Insert();
            GridViewBind();

        }
        private void doDeal()
        {
            taobaotrade ui = taobaotrade.Find("1=2");
            ui.New();
            ui.seller_nick.Value = brokerName.Text;
            ui.buyer_nick.Value = AliApi.appUserId;
           // ui.taobaoiid.Value = itemiidHidden.Value;
            ui.taobaoname.Value = "订阅  " + brokerName.Text;
            ui.taobaoprice.Value = hideAmount.Text;
            //.taobaopic_path.Value = ItemImage.ImageUrl;
            ui.taobaonum.Value = Convert.ToInt32("1");
            ui.taobaocreated.Value = DateTime.Now.ToString();
            ui.taobaotid.Value = ui.GetRecID().ToString();
            ui.taobaopayment.Value = hideAmount.Text;
            ui.taobaostatus.Value = TRADE_STATUS.
Buyer_Recieved;
            ui.taobaoseller_rate.Value = "false";
            ui.taobaobuyer_rate.Value = "false";
            ui.Notes.Value = "订阅  " + brokerName.Text;

            //string tranStr = PostTypeRadioButtonList.SelectedItem.Text;
            //int startIndex = tranStr.IndexOf("：");
            //int endIndex = tranStr.IndexOf("元");

            //ui.cbtrantype.Value = tranStr.Substring(0, startIndex);
            //ui.transfee.Value = tranStr.Substring(startIndex + 1, endIndex - 1 - startIndex - 1);
            ui.Insert();


//taobaotrade ui = taobaotrade.Find("1=2");
            cbuser u = cbuser.u();
            double need = zfunction.str2double(hideAmount.Text);
            double have = zfunction.str2double(u.cbmoney.ToString());
            if (have < need)
            {
                MessageBox.Show(this.UpdatePanel1, "你的余额不够.");
                return;
            }
            //if (ui.taobaostatus.ToString() != TRADE_STATUS.Buyer_Order)
            //{
            //    MessageBox.Show(this.UpdatePanel1, "该订单已经支付,无需再付款,操作取消.");
            //    return;
            //}
            cbmoneytrans mt = cbmoneytrans.Find("1=2");
            mt.New();
            mt.cbuserid.Value = AliApi.appUserId;
            mt.cbmoney.Value = -need;
            mt.cbmoneytrantype.Value = MONEY_TransType.Block;
            mt.jccreatedate.Value = DateTime.Now;
            mt.jccreatetime.StringValue = DateTime.Now.ToLongTimeString();
            //mt.jcdesc.Value = ui.RECID.ToString() + " " + ui.taobaoname.ToString();
            mt.jcdesc.Value = "订阅  " + brokerName.Text;
            mt.NoYes_dongjie.Value = RuleCode.Enum.NoYes.No;
            mt.RefTable.Value = "taobaotrade";
            mt.RefRecID.Value = "100001";
            mt.Insert();
            u.cbmoney.Value = u.cbmoney.Value - need;
            u.cbmoneyF.Value = u.cbmoneyF.Value - need;
            u.Modify();

            cbmoneytrans mt2 = cbmoneytrans.Find("1=2");
            mt2.New();
            mt2.cbuserid.Value = brokerName.Text;
            mt2.cbmoney.Value = need;
            mt2.cbmoneytrantype.Value = MONEY_TransType.Block;
            mt2.jccreatedate.Value = DateTime.Now;
            mt2.jccreatetime.StringValue = DateTime.Now.ToLongTimeString();
            mt2.jcdesc.Value = "订阅者  " + AliApi.appUserId;
            mt2.NoYes_dongjie.Value = RuleCode.Enum.NoYes.Yes;
            mt.RefTable.Value = "taobaotrade";
            mt.RefRecID.Value = "100001";
            mt2.Insert();

            cbuser u2 = cbuser.Find(" cbuserid = '" + ui.seller_nick.ToString() + "'");
            u2.cbmoneyD.Value += need;
            u2.Modify();

            
            ui.taobaostatus.Value = TRADE_STATUS.Buyer_Pay;
            ui.Modify();
            MessageBox.Show(this.UpdatePanel1, "操作成功,你支付的金额为:" + need.ToString());
        }
    }
}
